﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Forum.BLL.Post;
using Forum.Entity.Post;
using Frame.Lib.Utilities;

namespace Forum.Web.Admin.UForum.Board
{
    public partial class TopicTypeEdit : PageEdit
    {
        private TopicTypeBO TopicType
        {
            get
            {
                return TopicTypeBO.Instance<TopicTypeBO>();
            }
        }

        private string BoardID
        {
            get
            {
                return Request["BoardID"];
            }
        }



        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (PKID2 != null && PKID2 != "")
                {
                    DispInfo();
                }
                else
                {
                    Orders.Text = TopicType.GetMaxOrder().ToString();
                }
                WebUtil.SetSaveBtn(Request["ID"], btnSave);

            }
        }

        private void DispInfo()
        {
            TopicTypeInfo info = TopicType.GetInfo(PKID2);
            EntityUtil.SetControl(pnlMain, info);
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strName = Name.Text.Trim();
            TopicTypeInfo info = TopicType.GetInfo(PKID2);
            if (PKID2 == null)
            {
                info.BoardID = int.Parse(this.BoardID);
            }

            bool flag = TopicType.Exist(strName, PKID2, info.BoardID);
            if (flag)
            {
                JSUtil.Alert("该名称已存在");
            }
            else
            {
                EntityUtil.SetEntity(pnlMain, info);
                TopicType.Save(info);
                if (PKID2 == null)
                {
                    JSUtil.RefreshView(this);
                    ClearInfo();
                }
                else
                {
                    JSUtil.RefreshView();
                }
            }
        }

        private void ClearInfo()
        {
            PKID2 = null;
            Name.Text = "";
            Orders.Text = TopicType.GetMaxOrder().ToString(); ;
            Remark.Text = "";
        }
    }
}